package com.controller;

import com.constant.RedisKey;
import com.enums.ApiReturnCodeEnum;
import com.po.UserPO;
import com.service.SendCodeServiceImpl;
import com.service.UserServiceImpl;
import com.util.*;
import dto.UserDTO;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @author linyukun
 * @date 2022/7/6 16:22
 */
@RequestMapping("login")
@RestController
public class LoginController {

    @Autowired
    private SendCodeServiceImpl sendCodeServiceImpl;

    @Autowired
    private UserServiceImpl userServiceImpl;

    
     /**
       * @author 林钰坤
       * @date 2022-07-06 16:29
     */
    @ApiOperation(value = "获取图形验证码（林钰坤）",notes = "",httpMethod = "GET")
    @RequestMapping(value = "/imageCode",method = RequestMethod.GET)
    public void imageCode(HttpServletRequest request, HttpServletResponse response){
        String ip = IpUtils.getClientIpAddress(request);
        Map<String, Object> map = null;
        OutputStream os = null;
        try {
            os = response.getOutputStream();
            map = ImageCode.getImageCode(120, 60, os);//生成图片
        }catch (Exception e){
            throw new RestException(ApiReturnCodeEnum.CAPTCHA_ERROR);
        }
        String captcha = (String) map.get("strEnsure").toString().toLowerCase();
        sendCodeServiceImpl.sendCode(captcha,ip);
        try {
            ImageIO.write((BufferedImage) map.get("image"), "JPEG", os);
            os.close();
        } catch (IOException e) {
            throw new RestException(ApiReturnCodeEnum.CAPTCHA_ERROR);
        }
    }

    /**
     * @author 林钰坤
     * @date 2022-07-06 16:29
     */
    @ApiOperation(value = "登录接口（林钰坤）",notes = "",httpMethod = "POST")
    @RequestMapping(value = "/doLogin",method = RequestMethod.POST)
    public Result doLogin(@RequestBody UserDTO userDTO, HttpServletRequest request, HttpServletResponse response){
        try{
            UserPO result =  userServiceImpl.doLogin(userDTO,request);
            Map<String,String> resultMap = new HashMap<>();
            resultMap.put("userName" , result.getUserName());//首页界面的右上角展示的
            resultMap.put("userToken" , result.getUserToken());
            resultMap.put("userId" , result.getId()+ "");
            resultMap.put("isAdmin" , result.getIsAdmin()+ "");
            return ResultUtil.returnDataSuccess(resultMap);
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }


    /**
     * @author 林钰坤
     * @date 2022-07-07 10:13:00
     */
    @ApiOperation(value = "登出接口（林钰坤）",notes = "",httpMethod = "POST")
    @RequestMapping(value = "/outLogin",method = RequestMethod.POST)
    public void outLogin(HttpServletRequest request, HttpServletResponse response){
        try{
            userServiceImpl.outLogin(request);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}
